package presentation.web ;

import java.rmi.RemoteException ;
import java.util.Map ;

import jaxcent.HtmlButton ;
import jaxcent.HtmlInputText ;
import jaxcent.HtmlPara ;
import jaxcent.HtmlTextArea ;
import jaxcent.Jaxception ;
import presentation.UIDirectory ;
import presentation.UIThread ;
import util.Log ;
import util.NotFoundException ;
import util.Subsystem ;
import domain.user.Privilege ;
import domain.user.UnpermittedActionException ;

/**
 * Add thread page
 **/
public class AddThread extends MainPage
{
	/** the directory to which the thread will be added **/
	private UIDirectory currentDirectory ;

	/** place for error messages **/
	private HtmlPara errorsPara ;

	/** the message text **/
	private HtmlTextArea messageText ;

	/** the topic text **/
	private HtmlInputText topicName ;

	/**
	 * Instantiates a add thread page
	 */
	public AddThread()
	{
		super() ;
		try
		{
			if (!this.verifyUserAllowedPage(Privilege.ADD_THREAD))
			{
				return ;
			}

			this.currentDirectory = null ;
			try
			{
				Map<String, String> params = this.getPageParams() ;
				long dirId = Long.parseLong(params.get("dir")) ;
				this.currentDirectory = this.getForumLogic().getDirectory(dirId) ;
			}
			catch (Exception e)
			{
				Log.getLogger(Subsystem.PAGE_RENDER).fine(e.getClass().getName() + " occured while loading container directory.") ;
				this.navigateToErrorPage(e, null);
				return;
			}
			this.topicName = new HtmlInputText(this, "topicField") ;
			this.messageText = new HtmlTextArea(this, "messageField") ;
			this.makeButtons() ;
			this.setErrors() ;
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null);
		}

	}

	private void addThreadHandler()
	{
		try
		{
			String topic = this.topicName.getValue() ;
			String msg = this.messageText.getValue() ;
			long dirID = this.currentDirectory.getId() ;

			if (this.topicName.getValue() == null || this.topicName.getValue().equals(""))
			{
				// no topic insertion:
				this.errorsPara.setInnerHTML("please enter a topic!") ;
				return ;
			}
			if (this.messageText.getValue() == null || this.messageText.getValue().equals(""))
			{
				// no message insertion:
				this.errorsPara.setInnerHTML("please write the first message of this new topic") ;
				return ;
			}

			UIThread thread = this.getForumLogic().addThread(dirID, this.getUser().getId(), topic, msg) ;
			this.topicName.setValue("") ;
			this.messageText.setValue("") ;
			Log.getLogger(Subsystem.PAGE_RENDER).finest(
				"user " + this.getUser().getId() + " has added the thread " + topic + " with id "
					+ thread.getId()) ;
			this.errorsPara.setInnerHTML("") ;
			this.navigateToPage("view directory", "dir=" + this.currentDirectory.getId()) ;
		}
		catch (UnpermittedActionException e)
		{
			Log.getLogger(Subsystem.PAGE_RENDER).info("user is unpermitted to add thread.") ;
			this.navigateToErrorPage("you are not allowed to perform this operation", null);
		}
		catch (NotFoundException e)
		{
			Log.getLogger(Subsystem.PAGE_RENDER).info(
				"the wanted directory to add thread in it could not found.") ;
			this.navigateToErrorPage("Container directory has been deleted", null);
		}
		catch (Jaxception e)
		{
			Log.getLogger(Subsystem.PAGE_RENDER).info("jaxcent problem when trying to add thread.") ;
			this.navigateToErrorPage(e, null);
		}
		catch (RemoteException e)
		{
			Log.getLogger(Subsystem.PAGE_RENDER).info("communication problem when trying to add thread.") ;
			this.navigateToErrorPage(e, null);
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null);
		}

	}


	private void cancelHandler()
	{
		try
		{
			this.topicName.setValue("") ;
			this.messageText.setValue("") ;
			this.navigateToPage("view directory", "dir=" + this.currentDirectory.getId()) ;
		}
		catch (Exception e)
		{
			Log.getLogger(Subsystem.PAGE_RENDER).fine(e.getClass().getName() + " when navigating back to dir from cancel") ;
			this.navigateToErrorPage(e, this.topicName);
		}
	}

	private void makeButtons()
	{
		try
		{
			new HtmlButton(this, "addThread")
			{
				@SuppressWarnings("synthetic-access")
				@Override
				protected void onClick()
				{
					AddThread.this.addThreadHandler() ;
				}
			} ;
			new HtmlButton(this, "cancel")
			{
				@SuppressWarnings("synthetic-access")
				@Override
				protected void onClick()
				{
					AddThread.this.cancelHandler() ;
				}
			} ;
			new HtmlButton(this, "reset")
			{
				@SuppressWarnings("synthetic-access")
				@Override
				protected void onClick()
				{
					AddThread.this.resetHandler() ;
				}
			} ;
		}
		catch (Exception e)
		{
			this.navigateToErrorPage(e, null);
		}
	}

	private void resetHandler()
	{
		try
		{
			this.topicName.setValue("") ;
			this.messageText.setValue("") ;
		}
		catch (Exception e)
		{
			Log.getLogger(Subsystem.PAGE_RENDER).fine(e.getClass().getName() + " when reseting fields in the page") ;
			this.navigateToErrorPage(e, this.topicName);
		}
	}

	private void setErrors()
	{
		try
		{
			this.errorsPara = new HtmlPara(this, "errorField") ;
			this.errorsPara.setInnerHTML("") ;
		}
		catch (Exception e)
		{
			Log.getLogger(Subsystem.PAGE_RENDER).fine(e.getClass().getName() + " when setting error field") ;
			this.navigateToErrorPage(e, this.errorsPara);
		}
	}

}
